package com.panlin.daohang;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.core.app.ActivityCompat;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;

import java.util.List;

/**
 * Created by luofei on 2019/7/1.
 */

public class LocationProUtils {
    private volatile static LocationProUtils uniqueInstance;
    private LocationManager locationManager;
    private String locationProvider = null;
    private Location location = null;
    private Context mContext;
    private String TAG = "LocationUtils";
    private long mLocationUpdateTimes = 1000;
    private long mLocationDistance = 0;
    private boolean mGpsSwitch = false;
    private double tempLat = 65535;
    private double tempLon = 65535;
    private double tempAlt = 65535;
    private  long sendTime = 0;

    private LocationProUtils(Context context) {
        mContext = context;
        locationManager = (LocationManager) mContext.getSystemService( Context.LOCATION_SERVICE );
    }

    //采用Double CheckLock(DCL)实现单例
    public static LocationProUtils getInstance(Context context) {
        if (uniqueInstance == null) {
            synchronized (LocationProUtils.class) {
                if (uniqueInstance == null) {
                    uniqueInstance = new LocationProUtils( context );
                }
            }
        }
        return uniqueInstance;
    }



    public void initLocationManager() {
        List<String> providers = locationManager.getProviders( true );
        if (providers.contains( LocationManager.GPS_PROVIDER )) {
            locationProvider = LocationManager.GPS_PROVIDER;
        } else if (providers.contains( LocationManager.NETWORK_PROVIDER )) {
            locationProvider = LocationManager.NETWORK_PROVIDER;
        }
    }

    private void setLocation(Location location) {
        this.location = location;
        if(location!=null){
            setGpsForCamera();
        }
    }

    public void closeGps(){
        if(mGpsSwitch){
            removeLocationUpdatesListener();
            mGpsSwitch = false;
        }
    }

    public void registerLocation(){
        if (Build.VERSION.SDK_INT >= 23 &&
                ActivityCompat.checkSelfPermission( mContext, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission( mContext, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        List<String> providers = locationManager.getProviders( true);
        for (int i=0;i<providers.size();i++){
            location = locationManager.getLastKnownLocation( providers.get(i));
            Log.i(TAG,"location-gps providers.get(i):"+providers.get(i)+" ,location:"+(location == null));
            if(location!=null){
                locationProvider=providers.get(i);
                setLocation( location );
                break;
            }
        }
        if(locationProvider!=null){
            Log.i(TAG,"location-gps locationProvider:"+locationProvider);
            requestLocation(providers);
        }else {
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            String provider = locationManager.getBestProvider(criteria, true);
            if(!StringUtils.isEmpty(provider)){
                requestLocation(providers);
                locationManager.requestLocationUpdates(provider, mLocationUpdateTimes, mLocationDistance, locationListener);
            }
        }
    }
    @SuppressLint("MissingPermission")
    private void requestLocation(List<String> providers){
        if(providers==null){
            return;
        }
        providers.forEach(data->{
            locationManager.requestLocationUpdates( data, mLocationUpdateTimes, mLocationDistance, locationListener );
        });
    }
    public Location showLocation() {
        return location;
    }

    private void removeLocationUpdatesListener() {
        if (Build.VERSION.SDK_INT >= 23 &&
                ActivityCompat.checkSelfPermission( mContext, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission( mContext, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        if (locationManager != null) {
            if(locationProvider!=null){
                locationManager.removeUpdates( locationListener );
            }
            locationProvider = null;
        }
    }


    private void showToast(final String msg) {
//        AppToast.show(mContext, msg, 3000);
//        Log.i("gps_test====msg==="+msg);
    }

    LocationListener locationListener = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle arg2) {
            showToast("gps 提供发生变化");
        }

        @Override
        public void onProviderEnabled(String provider) {
            showToast("gps 设备已打开");
        }

        @Override
        public void onProviderDisabled(String provider) {
            showToast("gps 设备已关闭");
        }

        @Override
        public void onLocationChanged(Location location) {
            setLocation( location );
        }
    };

    @SuppressLint("Range")
    public void setGpsForCamera() {
        double mLatitude = 65535;
        double mLongitude = 65535;
        double mAltitude = 65535;
        if (location != null){
            mLatitude=location.getLatitude();
            mLongitude=location.getLongitude();
            mAltitude=location.getAltitude();
            LogUtils.i("setGpsForCamera mLatitude:"+mLatitude+" ,mLongitude:"+mLongitude);
        }
        if (tempAlt == mAltitude && tempLat == mLatitude && tempLon == mLongitude) {
            return;
        }

        if (System.currentTimeMillis() - sendTime < 1000) {
            return;
        }
    }
}
